package com.yc.fav.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.yc.fav.entity.Tag;
import com.yc.fav.entity.User;
import com.yc.fav.dao.UserMapper;
import com.yc.fav.service.BizException;
import com.yc.fav.service.IUserService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yc.fav.util.MBHelper;
import com.yc.fav.util.Utils;
import org.springframework.stereotype.Service;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author k
 * @since 2024-10-28
 */
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements IUserService {

    @Override
    public User login(String username, String password) throws BizException {
        if(username==null||username.isBlank()){
            throw new BizException("请输入用户名",-2);
        }
        if(password==null||password.isBlank()){
            throw new BizException("请输入密码",-3);
        }
        if(password.length()<3){
            throw new BizException("密码不能小于3位",-4);
        }
            User user= MBHelper.execute(sqlSession -> {
            UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
            LambdaQueryWrapper<User> lqw = new LambdaQueryWrapper<>();
            lqw.eq(User::getName,username);
            lqw.eq(User::getPwd,password);
            return userMapper.selectOne(lqw);
        });
        if (user==null){
            throw new BizException("用户或密码错误",-1);
        }
        return user;
    }

    @Override
    public void reg(User user) throws BizException {
        Utils.checkEmpty(user.getName(),"用户名不能为空",-1);
        Utils.checkEmpty(user.getPwd(),"密码不能为空",-2);
        Utils.check(user.getPwd().length()<3,"密码不能小于3位",-5);
        Utils.checkEmpty(user.getEmail(),"邮箱不能为空",-3);
        String email = user.getEmail();
        //判断是否符合邮箱格式
        String emailRegex = "^[a-zA-Z0-9_.+-]+@[a-zA-Z0-9-]+\\.[a-zA-Z0-9-.]+$";
        Boolean emailValied= email.matches(emailRegex);
        Utils.check(!emailValied,"邮箱格式不正确",-4);
        //判断用户名是否重复
        Boolean panName=MBHelper.execute(sqlSession -> {
           UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
            LambdaQueryWrapper<User> lqw=new LambdaQueryWrapper<>();
            lqw.eq(User::getName,user.getName());
            User dbUser=userMapper.selectOne(lqw);
            if(dbUser==null){
                return true;
            }
            return false;
        });
        Utils.check(!panName,"用户名已存在",-6);
        //判断邮箱是否重复
        Boolean panEmail=MBHelper.execute(sqlSession -> {
            UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
            LambdaQueryWrapper<User> lqw=new LambdaQueryWrapper<>();
            lqw.eq(User::getEmail,user.getEmail());
            User dbUser=userMapper.selectOne(lqw);
            if(dbUser==null){
                return true;
            }
            return false;
        });
        Utils.check(!panEmail,"邮箱已存在",-7);
        MBHelper.execute(sqlSession -> {
           UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
           userMapper.insert(user);
           return null;
        });
    }
}
